/*
 * MailManGUI.java
 *
 * Created on 18 de Julho de 2008, 14:56
 */

package pt.citar.diablu.mailman.gui;


import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import pt.citar.diablu.mailman.MailMan;
import pt.citar.diablu.mailman.util.datastructures.MailManLog;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.swing.JFileChooser;


public class MailManGUI extends javax.swing.JFrame {
    
    public final static int START = 0;
    public final static int STOP = 1;
    
    private MailMan mailman;
    private int connectionButtonState;
    
         
    /**
     * Creates new form MailManGUI
     */
    public MailManGUI(MailMan mailman) {
        
        this.mailman = mailman;
        this.connectionButtonState = START;
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        directoryChooser = new javax.swing.JFileChooser();
        MailManTabbedPane = new javax.swing.JTabbedPane();
        ConnectionTab = new javax.swing.JPanel();
        MailManPanel = new javax.swing.JPanel();
        MailManPortLabel = new javax.swing.JLabel();
        MailManPortTextField = new javax.swing.JTextField();
        YourAppPanel = new javax.swing.JPanel();
        YourAppHostnameLabel = new javax.swing.JLabel();
        YourAppHostnameTextField = new javax.swing.JTextField();
        YourAppPortLabel = new javax.swing.JLabel();
        YourAppPortTextField = new javax.swing.JTextField();
        ConnectionButton = new javax.swing.JButton();
        RepositoryTab = new javax.swing.JPanel();
        RepositoryPanel = new javax.swing.JPanel();
        RepositoryLabel = new javax.swing.JLabel();
        RepositoryTextField = new javax.swing.JTextField();
        RepositoryBrowseButton = new javax.swing.JButton();
        LogTab = new javax.swing.JPanel();
        LogScrollPane = new javax.swing.JScrollPane();
        LogTextPane = new javax.swing.JTextPane();
        LogOptionsPanel = new javax.swing.JPanel();
        BTFileTransfersCheckBox = new javax.swing.JCheckBox();
        OSCMessagesCheckBox = new javax.swing.JCheckBox();
        DetectedDevicesCheckBox = new javax.swing.JCheckBox();
        OtherCheckBox = new javax.swing.JCheckBox();
        AboutPanel = new javax.swing.JPanel();
        AboutLabel = new javax.swing.JLabel();

        directoryChooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("DiABlu MailMan");
        setResizable(false);

        MailManPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("MailMan"));

        MailManPortLabel.setText("Port:");
        MailManPortLabel.setPreferredSize(new java.awt.Dimension(54, 14));

        MailManPortTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        MailManPortTextField.setText(mailman.getProperties().getProperty("IncomingPort", "12000"));
        MailManPortTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                MailManPortTextFieldKeyReleased(evt);
            }
        });

        org.jdesktop.layout.GroupLayout MailManPanelLayout = new org.jdesktop.layout.GroupLayout(MailManPanel);
        MailManPanel.setLayout(MailManPanelLayout);
        MailManPanelLayout.setHorizontalGroup(
            MailManPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(MailManPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(MailManPortLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(MailManPortTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 69, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(566, Short.MAX_VALUE))
        );
        MailManPanelLayout.setVerticalGroup(
            MailManPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(MailManPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(MailManPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(MailManPortLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(MailManPortTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        YourAppPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Your Application"));

        YourAppHostnameLabel.setText("Hostname:");

        YourAppHostnameTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        YourAppHostnameTextField.setText(mailman.getProperties().getProperty("RemoteIP", "127.0.0.1"));
        YourAppHostnameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                YourAppHostnameTextFieldKeyReleased(evt);
            }
        });

        YourAppPortLabel.setText("Port:");

        YourAppPortTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        YourAppPortTextField.setPreferredSize(new java.awt.Dimension(59, 20));
        YourAppPortTextField.setText(mailman.getProperties().getProperty("OutgoingPort", "12001"));
        YourAppPortTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                YourAppPortTextFieldKeyReleased(evt);
            }
        });

        org.jdesktop.layout.GroupLayout YourAppPanelLayout = new org.jdesktop.layout.GroupLayout(YourAppPanel);
        YourAppPanel.setLayout(YourAppPanelLayout);
        YourAppPanelLayout.setHorizontalGroup(
            YourAppPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(YourAppPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(YourAppHostnameLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(YourAppHostnameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 69, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(51, 51, 51)
                .add(YourAppPortLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(YourAppPortTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(420, Short.MAX_VALUE))
        );

        YourAppPanelLayout.linkSize(new java.awt.Component[] {YourAppHostnameTextField, YourAppPortTextField}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        YourAppPanelLayout.setVerticalGroup(
            YourAppPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(YourAppPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(YourAppPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(YourAppHostnameLabel)
                    .add(YourAppPortLabel)
                    .add(YourAppPortTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(YourAppHostnameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        ConnectionButton.setText("Start Server");
        ConnectionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConnectionButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout ConnectionTabLayout = new org.jdesktop.layout.GroupLayout(ConnectionTab);
        ConnectionTab.setLayout(ConnectionTabLayout);
        ConnectionTabLayout.setHorizontalGroup(
            ConnectionTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(ConnectionTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(ConnectionTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(YourAppPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, ConnectionButton)
                    .add(MailManPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        ConnectionTabLayout.setVerticalGroup(
            ConnectionTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(ConnectionTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(MailManPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(3, 3, 3)
                .add(YourAppPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(5, 5, 5)
                .add(ConnectionButton)
                .addContainerGap(85, Short.MAX_VALUE))
        );

        MailManTabbedPane.addTab("Connection", ConnectionTab);

        RepositoryPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Repository"));

        RepositoryLabel.setText("Directory:");
        RepositoryLabel.setMaximumSize(new java.awt.Dimension(54, 14));
        RepositoryLabel.setPreferredSize(new java.awt.Dimension(54, 14));

        try{     RepositoryTextField.setText(mailman.getProperties().getProperty("Directory", new File(".").getCanonicalPath().toString())); } catch (IOException ex) {     Logger.getLogger(MailMan.class.getName()).log(Level.SEVERE, null, ex); }
        RepositoryTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                RepositoryTextFieldKeyReleased(evt);
            }
        });

        RepositoryBrowseButton.setText("Browse...");
        RepositoryBrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RepositoryBrowseButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout RepositoryPanelLayout = new org.jdesktop.layout.GroupLayout(RepositoryPanel);
        RepositoryPanel.setLayout(RepositoryPanelLayout);
        RepositoryPanelLayout.setHorizontalGroup(
            RepositoryPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, RepositoryPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(RepositoryLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(RepositoryTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 511, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(RepositoryBrowseButton)
                .addContainerGap())
        );
        RepositoryPanelLayout.setVerticalGroup(
            RepositoryPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(RepositoryPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(RepositoryPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(RepositoryBrowseButton)
                    .add(RepositoryTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(RepositoryLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout RepositoryTabLayout = new org.jdesktop.layout.GroupLayout(RepositoryTab);
        RepositoryTab.setLayout(RepositoryTabLayout);
        RepositoryTabLayout.setHorizontalGroup(
            RepositoryTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(RepositoryTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(RepositoryPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        RepositoryTabLayout.setVerticalGroup(
            RepositoryTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(RepositoryTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(RepositoryPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(195, Short.MAX_VALUE))
        );

        MailManTabbedPane.addTab("Repository", RepositoryTab);

        LogScrollPane.setViewportView(LogTextPane);

        LogOptionsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Log Options"));

        BTFileTransfersCheckBox.setSelected(true);
        BTFileTransfersCheckBox.setText("Bluetooth File Transfers");
        BTFileTransfersCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        BTFileTransfersCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        BTFileTransfersCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                BTFileTransfersCheckBoxStateChanged(evt);
            }
        });

        OSCMessagesCheckBox.setSelected(true);
        OSCMessagesCheckBox.setText("OSC Messages");
        OSCMessagesCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        OSCMessagesCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        OSCMessagesCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                OSCMessagesCheckBoxStateChanged(evt);
            }
        });

        DetectedDevicesCheckBox.setSelected(true);
        DetectedDevicesCheckBox.setText("Detected Devices");
        DetectedDevicesCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        DetectedDevicesCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        DetectedDevicesCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                DetectedDevicesCheckBoxStateChanged(evt);
            }
        });

        OtherCheckBox.setSelected(true);
        OtherCheckBox.setText("Other");
        OtherCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        OtherCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        OtherCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                OtherCheckBoxStateChanged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout LogOptionsPanelLayout = new org.jdesktop.layout.GroupLayout(LogOptionsPanel);
        LogOptionsPanel.setLayout(LogOptionsPanelLayout);
        LogOptionsPanelLayout.setHorizontalGroup(
            LogOptionsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(LogOptionsPanelLayout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(BTFileTransfersCheckBox)
                .add(70, 70, 70)
                .add(OSCMessagesCheckBox)
                .add(26, 26, 26)
                .add(DetectedDevicesCheckBox)
                .add(39, 39, 39)
                .add(OtherCheckBox)
                .add(118, 118, 118))
        );

        LogOptionsPanelLayout.linkSize(new java.awt.Component[] {BTFileTransfersCheckBox, DetectedDevicesCheckBox, OSCMessagesCheckBox, OtherCheckBox}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        LogOptionsPanelLayout.setVerticalGroup(
            LogOptionsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(LogOptionsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(LogOptionsPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(BTFileTransfersCheckBox)
                    .add(OSCMessagesCheckBox)
                    .add(DetectedDevicesCheckBox)
                    .add(OtherCheckBox))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout LogTabLayout = new org.jdesktop.layout.GroupLayout(LogTab);
        LogTab.setLayout(LogTabLayout);
        LogTabLayout.setHorizontalGroup(
            LogTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, LogTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(LogTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, LogOptionsPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 719, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, LogScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 719, Short.MAX_VALUE))
                .addContainerGap())
        );
        LogTabLayout.setVerticalGroup(
            LogTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(LogTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(LogScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 190, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(LogOptionsPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(30, 30, 30))
        );

        MailManTabbedPane.addTab("Logs", LogTab);

        AboutLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        AboutLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aboutMailman.png"))); // NOI18N

        org.jdesktop.layout.GroupLayout AboutPanelLayout = new org.jdesktop.layout.GroupLayout(AboutPanel);
        AboutPanel.setLayout(AboutPanelLayout);
        AboutPanelLayout.setHorizontalGroup(
            AboutPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(AboutPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(AboutLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 719, Short.MAX_VALUE)
                .addContainerGap())
        );
        AboutPanelLayout.setVerticalGroup(
            AboutPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(AboutPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(AboutLabel)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        MailManTabbedPane.addTab("About", AboutPanel);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(MailManTabbedPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 744, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, MailManTabbedPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
        );

        MailManTabbedPane.getAccessibleContext().setAccessibleName("");
        MailManTabbedPane.getAccessibleContext().setAccessibleDescription("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void YourAppPortTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_YourAppPortTextFieldKeyReleased
        FileOutputStream fos = null;
        try {
            File file = new File("config.ini");
            fos = new FileOutputStream(file);
            mailman.getProperties().setProperty("OutgoingPort", YourAppPortTextField.getText());
            mailman.getProperties().store(fos, "Mailman Properties");
        } catch (IOException ex) {
            Logger.getLogger(MailManGUI.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(MailManGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_YourAppPortTextFieldKeyReleased

    private void YourAppHostnameTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_YourAppHostnameTextFieldKeyReleased
        FileOutputStream fos = null;
        try {
            File file = new File("config.ini");
            fos = new FileOutputStream(file);
            mailman.getProperties().setProperty("RemoteIP", YourAppHostnameTextField.getText());
            mailman.getProperties().store(fos, "Mailman Properties");
        } catch (IOException ex) {
            Logger.getLogger(MailManGUI.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(MailManGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_YourAppHostnameTextFieldKeyReleased

    private void MailManPortTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_MailManPortTextFieldKeyReleased
        FileOutputStream fos = null;
        try {
            File file = new File("config.ini");
            fos = new FileOutputStream(file);
            mailman.getProperties().setProperty("OutgoingPort", MailManPortTextField.getText());
            mailman.getProperties().store(fos, "Mailman Properties");
        } catch (IOException ex) {
            Logger.getLogger(MailManGUI.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(MailManGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_MailManPortTextFieldKeyReleased

    private void OtherCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_OtherCheckBoxStateChanged
        updateLogLevel();
    }//GEN-LAST:event_OtherCheckBoxStateChanged

    private void DetectedDevicesCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_DetectedDevicesCheckBoxStateChanged
        updateLogLevel();
    }//GEN-LAST:event_DetectedDevicesCheckBoxStateChanged

    private void OSCMessagesCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_OSCMessagesCheckBoxStateChanged
        updateLogLevel();
    }//GEN-LAST:event_OSCMessagesCheckBoxStateChanged

    private void BTFileTransfersCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_BTFileTransfersCheckBoxStateChanged
        updateLogLevel();
    }//GEN-LAST:event_BTFileTransfersCheckBoxStateChanged

    private void RepositoryBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RepositoryBrowseButtonActionPerformed
        directoryChooser.setCurrentDirectory(new File(RepositoryTextField.getText()));    
        if (directoryChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
        {                                        
            RepositoryTextField.setText(directoryChooser.getSelectedFile().toString());
            FileOutputStream fos = null;
            try {
                File file = new File("config.ini");
                fos = new FileOutputStream(file);
                mailman.getProperties().setProperty("Directory", RepositoryTextField.getText());
                mailman.getProperties().store(fos, "Mailman Properties");
            } catch (IOException ex) {
                Logger.getLogger(MailManGUI.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(MailManGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }//GEN-LAST:event_RepositoryBrowseButtonActionPerformed

    private void RepositoryTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_RepositoryTextFieldKeyReleased
        FileOutputStream fos = null;
        try {
            File file = new File("config.ini");
            fos = new FileOutputStream(file);
            mailman.getProperties().setProperty("Directory", RepositoryTextField.getText());
            mailman.getProperties().store(fos, "Mailman Properties");
        } catch (IOException ex) {
            Logger.getLogger(MailManGUI.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(MailManGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_RepositoryTextFieldKeyReleased

    private void ConnectionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConnectionButtonActionPerformed
        switch (connectionButtonState)
        {
            case START:
                mailman.getOsc().start();
                break;
            case STOP:
                mailman.getOsc().stop();
        }
    }//GEN-LAST:event_ConnectionButtonActionPerformed
    
    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel AboutLabel;
    private javax.swing.JPanel AboutPanel;
    private javax.swing.JCheckBox BTFileTransfersCheckBox;
    private javax.swing.JButton ConnectionButton;
    private javax.swing.JPanel ConnectionTab;
    private javax.swing.JCheckBox DetectedDevicesCheckBox;
    private javax.swing.JPanel LogOptionsPanel;
    private javax.swing.JScrollPane LogScrollPane;
    private javax.swing.JPanel LogTab;
    private javax.swing.JTextPane LogTextPane;
    private javax.swing.JPanel MailManPanel;
    private javax.swing.JLabel MailManPortLabel;
    private javax.swing.JTextField MailManPortTextField;
    private javax.swing.JTabbedPane MailManTabbedPane;
    private javax.swing.JCheckBox OSCMessagesCheckBox;
    private javax.swing.JCheckBox OtherCheckBox;
    private javax.swing.JButton RepositoryBrowseButton;
    private javax.swing.JLabel RepositoryLabel;
    private javax.swing.JPanel RepositoryPanel;
    private javax.swing.JPanel RepositoryTab;
    private javax.swing.JTextField RepositoryTextField;
    private javax.swing.JLabel YourAppHostnameLabel;
    private javax.swing.JTextField YourAppHostnameTextField;
    private javax.swing.JPanel YourAppPanel;
    private javax.swing.JLabel YourAppPortLabel;
    private javax.swing.JTextField YourAppPortTextField;
    private javax.swing.JFileChooser directoryChooser;
    // End of variables declaration//GEN-END:variables
    
    public String getServerPort()
    {
        return MailManPortTextField.getText();
    }
        
    public String getIpAddress()
    {
        return YourAppHostnameTextField.getText();
    }
    
    public String getClientPort()
    {
        return YourAppPortTextField.getText();
    }

    public void changeStartButtonState(int state)
    {
        connectionButtonState = state;
        switch (connectionButtonState)
        {
            case START:
                ConnectionButton.setText("Start Server");
                MailManPortTextField.setEnabled(true);
                break;
            case STOP:
                ConnectionButton.setText("Stop Server");
                MailManPortTextField.setEnabled(false);
        }
    
    }

    
    public String getDirectory()
    {
        return RepositoryTextField.getText();
    }
    
    public void updateLogLevel()
    {
        
        int i = 0;
        if(BTFileTransfersCheckBox.isSelected())
            i +=1;
        if(OSCMessagesCheckBox.isSelected())
            i +=2;
        if(DetectedDevicesCheckBox.isSelected())
            i +=4;
        if(OtherCheckBox.isSelected())
            i +=8;
        mailman.getLogger().setLevel(i);
        
    }
    
     public void updateLogTextPane()
     {
         String text = "";
        
        for(MailManLog l: mailman.getLogger().getLogs().getList())
        {
            if(mailman.getLogger().checkLevel(l))
                text += l.getMsg() + "\n";

        }
        
        LogTextPane.setText(text);
        if(text.length() > 0)
            LogTextPane.setCaretPosition(text.length()-1);
     }
     
}
